package cn.pzaily.orange.core.utils.sql;

import java.io.File;

/**
 * @PackageName: cn.pzaily.orange.backup.utils
 * @Description:
 * @Author: Pang
 * @Date: 2021-05-14 14:07
 */
public class MySqlBackupRestoreUtils {

    /**
     *
     * @param host
     * @param username
     * @param password
     * @param backupFolderPath
     * @param fileName
     * @param database
     * @return
     * @throws Exception
     */
    public static boolean backup(String host, String username, String password, String backupFolderPath, String fileName, String database) throws Exception {
        File file = new File(backupFolderPath);
        if (!file.exists()) {file.mkdirs();}
        if (!backupFolderPath.endsWith(File.separator) && !backupFolderPath.endsWith("/")) {
            backupFolderPath = backupFolderPath + File.separator;
        }

        String backupFilePath = backupFolderPath + fileName;
        StringBuilder builder = new StringBuilder();
        builder.append("mysqldump --opt ").append(" --add-drop-database ").append("  --add-drop-table");
        builder.append(" -h").append(host).append(" -u").append(username).append(" -p").append(password);
        builder.append(" --result-file=").append(backupFilePath).append(" --default-character-set=utf8 ").append(database);

        Process process = Runtime.getRuntime().exec(getCommand(builder.toString()));
        if (process.waitFor() == 0) {
            System.out.println("数据已经备份到 " + backupFilePath + "文件中");
            return true;
        }
        return false;
    }

    private static String[] getCommand(String command) {
        // 获取操作系统的类型
        String os = System.getProperty("os.name");
        // linux
        String shell = "/bin/bash";
        String c = "-c";
        // win
        if (os.toLowerCase().startsWith("win")) {
            shell = "cmd";
            c = "/c";
        }
        String[] cmd = {shell, c, command};
        return cmd;
    }

    /**
     *
     * @param restoreFilePath
     * @param host
     * @param username
     * @param password
     * @return
     */
    public static boolean restore(String restoreFilePath, String host, String username, String password, String database)  throws Exception {
        File file = new File(restoreFilePath);
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                if (listFile.exists() && listFile.getPath().endsWith(".sql")) {
                    restoreFilePath = listFile.getAbsolutePath();
                }
            }
        }
        StringBuilder builder = new StringBuilder();
        builder.append("mysql -h").append(host).append(" -u").append(username).append(" -p").append(password);
        builder.append(" ").append(database).append("<").append(restoreFilePath);

        Process exec = Runtime.getRuntime().exec(getCommand(builder.toString()));
        if (exec.waitFor() == 0) {
            System.out.println("数据已从" + restoreFilePath + "导入到" + database + "数据库中");
            return true;
        }

        return false;
    }

    public static void main(String[] args) throws Exception {


    }
}
